﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;

namespace Swsk33.SimpleArcEngine.Dialog
{
	public partial class IDWDialog : Form
	{
		/// <summary>
		/// 传入地图控件
		/// </summary>
		private AxMapControl mapControl;

		private IFeatureLayer featureLayer;

		private string field;

		private string outputPathString;

		/// <summary>
		/// 选择的图层
		/// </summary>
		public IFeatureLayer FeatureLayer { get => featureLayer; set => featureLayer = value; }

		/// <summary>
		/// 选择的字段
		/// </summary>
		public string Field { get => field; set => field = value; }

		/// <summary>
		/// 输出路径
		/// </summary>
		public string OutputPathString { get => outputPathString; set => outputPathString = value; }

		public IDWDialog(AxMapControl mapControl)
		{
			this.mapControl = mapControl;
			InitializeComponent();
		}

		private void ok_Click(object sender, System.EventArgs e)
		{
			if (outputPath.Text.Equals(""))
			{
				MessageBox.Show("输出路径不可为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			outputPathString = outputPath.Text;
			DialogResult = DialogResult.OK;
			Close();
		}

		private void saveFile_Click(object sender, System.EventArgs e)
		{
			FolderBrowserDialog dialog = new FolderBrowserDialog();
			dialog.Description = "请选择要保存的目录";
			if (dialog.ShowDialog() == DialogResult.OK)
			{
				outputPath.Text = dialog.SelectedPath;
			}
		}

		private void IDWDialog_Load(object sender, System.EventArgs e)
		{
			for (int i = 0; i < mapControl.LayerCount; i++)
			{
				if (((IFeatureLayer)mapControl.get_Layer(i)).FeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
				{
					layerBox.Items.Add(mapControl.get_Layer(i).Name);
				}
			}
			if (layerBox.Items.Count == 0)
			{
				MessageBox.Show("请至少加入一个点图层！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				Close();
				return;
			}
			else
			{
				layerBox.SelectedIndex = 0;
			}
		}

		/// <summary>
		/// 设定字段下拉框
		/// </summary>
		private void setFieldBox()
		{
			fieldBox.Items.Clear();
			for (int i = 0; i < featureLayer.FeatureClass.Fields.FieldCount; i++)
			{
				if (!featureLayer.FeatureClass.Fields.Field[i].Name.Equals("Shape"))
				{
					fieldBox.Items.Add(featureLayer.FeatureClass.Fields.Field[i].Name);
				}
			}
			fieldBox.SelectedIndex = 0;
		}

		private void layerBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			for (int i = 0; i < mapControl.LayerCount; i++)
			{
				if (mapControl.get_Layer(i).Name.Equals(layerBox.SelectedItem.ToString()))
				{
					featureLayer = (IFeatureLayer)mapControl.get_Layer(i);
					break;
				}
			}
			setFieldBox();
		}

		private void fieldBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			field = fieldBox.SelectedItem.ToString();
		}
	}
}